// pages/staff/allcourse.js
const app = getApp()
var CONFIG = require("../../config/config.js");
var share = require('../../utils/share.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list:[],
        page:0,
        code:'',
        msg:'',
        keyword:'',
        type:'',
        is_dl:0,
        typearr:[{title:'换课列表',id:2},{title:'退费列表',id:1}],
        isType:2,
    },

    clicktap(e) {
        var that = this;
        var item = e.target.dataset.item || e.currentTarget.dataset.item;
        that.setData({
            isType: item.id,
            msg: '',
            page: 0,
            list: [],
        })
        that.consCourses();
    },

    bindclick(e){
        var that = this;
        // console.log(e);
        var type = that.data.isType;
        var item = e.currentTarget.dataset.item;
        if (Number(type)==3){
            // var id = item.id;
            // wx.navigateTo({
            //     url: '../public/paylog?uc_id='+id+'',
            // })
        }else{
            var id = item.audit_id;
            wx.navigateTo({
                url: '/pages/apply/approval/approvaldetail?id='+id+'&leixing='+type+'&coursetype=1',
            })
        }
    },

    // 点击查询
    searchclick(e){
        var that = this;
        var key = e.detail.value.keykown;
        that.setData({
            msg: '',
            page: 0,
            list: [],
            keyword:key,
        })
        // console.log(key);
        that.consCourses();
    },

    consCourses(){
        wx.showLoading({
            title: '加载中~',
            mask: true
        })
        let that = this;
        var type = that.data.isType;
        
        var consultant_id = app.globalData.admin_id;
        var page = that.data.page;
        var keyword = that.data.keyword;
        wx.setStorageSync('keylistphone', keyword);
        
        var obj = {
            consultant_id: consultant_id,
            nowPage: page,
            type:type,
            keyword: keyword,
        }
        var url = 'crmapi/user/changeRefundList'
        share.wxRequest(url,obj,function(res){
            wx.hideLoading();
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                var newslist = data;
                if (Number(page) == 0) {
                    var lists = newslist;
                } else {
                    var lists = that.data.list.concat(newslist);
                }
                
                if(Number(lists.length)==0){
                    var msg = '~暂无数据~'
                }else{
                    var msg = res.data.msg
                }
                that.setData({
                    code: res.data.errcode,
                    msg: msg,
                    list: lists,
                })
            } else {
                // console.log(that.data.list)
                that.setData({
                    code: res.data.errcode,
                    msg: res.data.msg,
                    list: [],
                })
            }
        })
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        that.setData({
            is_dl:Number(app.globalData.is_dl)
        })
        wx.setNavigationBarTitle({
            title: '退换课列表',
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
        if (wx.getStorageSync('keylistphone')) {
            // console.log(8989);
            that.setData({
                keyword: wx.getStorageSync('keylistphone')
            })
        }else{
            that.setData({
                keyword: ''
            }) 
        }
        that.consCourses();
        
    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        this.setData({
            type:'',
        })
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        this.setData({
            type: '',
        })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        var that = this;
        if (wx.getStorageSync('keylistphone')) {
            wx.removeStorageSync('keylistphone');
        }
        that.setData({
            page: 0,
            keyword:'',
        })
        that.consCourses();
        wx.stopPullDownRefresh();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        var page = Number(that.data.page) +1;
        that.setData({
            page:page,
        })
        that.consCourses();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})